#include "BLDC_condition_judgement.h"
u16 count_for_condition_judgement = 0;

int16_t rise_end_voltage[3], down_begin_voltage[3], error_rise_down[3], pole_begin_voltage[3][6], pole_mid_voltage[3][6], pole_end_voltage[3][6];
int32_t error_sum = 0;
//定义是否已经计算完成
char calculated_speed_control = 12;
//中点计算的外部引用
extern u16 calculate_PID_output_by_time_error, calculate_pid_output_by_voltage_error, nagetive_feedback_by_time_error;
//电流差值检测
int pole_end_current[3][6];
int error_of_current[3], error_of_down_end_sub_middle_current, sum_of_phase_current = 0, error_sum_of_current = 0, current_target_min = 0, current_target_max = 0;
u16 duty_calculated_from_current = 0;
char voiding_current_duty_calculated = 0; // 设定屏蔽相应的电压抬升时间以免震荡
char rise_down_flat_flag[3][6], i_pole_get, i_check_get;
int max_current_temp[3], max_current_temp_read[3];
//穿越零点的计算
double hardware_specification = 2 * 3.14159 * 1500 * 4700 * 68 / 1000000000 / 6200;
double frequency_specification = 0, phase_angle_delay_specification = 0, voltage_for_crossing_electron_specification = 0, VFCES_now = 0;
u16 rising_crossing_time[3], falling_crossing_time[3], last_voltage[3], voltage_now[3], time_error_for_crossing_specification = 0;
int32_t voltage_after_crrection[3];
int16_t crossing_time_error[3], crossing_time_error_sum, voltage_for_crossing_flag[3], last_voltage_for_crossing_flag[3], crossing_time_sum;
char checked_crossing_flag[3];
// PID以及过零双电压平衡检测
u16 check_per_circle_flag = 0, condition_judgement_setting = 0, condition_judgement_calculated = 0, reserving_time_after_correction = 0, limit_reserving_time = 0, half_limit_reserving_time = 0, half_limit_reserving_time_after_subb = 0;
u16 check_voltage_diff_flag = 0, half_condition_judgement_calculated = 0;
u32 per_frequency_for_calculation = 0;
double kp_setting = 0, ki_setting = 0, kd_setting = 0;
double error_sum_in_rising = 0, error_sum_in_falling = 0, error_out_loop_correction = 0;
extern pole_status pole_status_set;
int16_t visual_pole_status_constant[3], visual_pole_status_for_calculation[3];
extern char write_in_calculate_result, calculate_current_PID_now, calculate_PID_now;
//从计算数值处引用
extern char overflow_check_flag;
extern u32 voltage_adc_8in1[3], current_adc_8in1[3];
extern u32 voltage_adc_2in1[3], voltage_adc_2in1_old[3], current_adc_2in1[3];
extern u16 *p_voltage_adc_1in1_u, *p_voltage_adc_1in1_v, *p_voltage_adc_1in1_w, *p_current_adc_1in1_u, *p_current_adc_1in1_v, *p_current_adc_1in1_w;
extern u16 voltage_adc_1in1[3][6][64], current_adc_1in1[3][6][64];
// extern u32 voltage_adc_1in1[3], voltage_adc_1in1_old[3], current_adc_1in1[3];
extern u32 voltage_adc_average_8in1[3], voltage_adc_average_8in1_sum[3], voltage_adc_average_8in1_average[3], voltage_adc_average_2in1_average[3], voltage_adc_average_1in1_average[3];
extern int32_t current_adc_average_8in1[3], current_adc_average_8in1_sum[3], current_adc_average_8in1_average[3], current_adc_average_2in1_average[3], current_adc_average_1in1_average[3];
extern pid_virables pid_voltage_error_controller, pid_current_error_controller, pid_time_set_controller;
char check_stages_for_calculation = 0;                               // 0
char error_flag = 0, error_check_double = 0, error_check_number = 0; //误差检测
u32 duty_add_per_cycle = 0, duty_limit_setting = 0;                  //自动提升电压
char use_inside_calculate_reserving = 0;                             // 0不计算，1计算
char direction_of_motor = 0;                                         // 0反转，1正转
//清零计数器

void adc_count_clean()
{
    count_for_condition_judgement = 0;
}
//设置采样和计算点
void calculate_new_check_time(void)
{
    check_voltage_diff_flag = condition_judgement_calculated >> 2;
    half_condition_judgement_calculated = condition_judgement_calculated >> 1;
    per_frequency_for_calculation = 45000 / (condition_judgement_calculated + 150);
}
//外部设定换向时间
void set_condition_judgement_setting(u16 condition_judgement_setting_in)
{
    condition_judgement_setting = condition_judgement_setting_in;
    condition_judgement_calculated = condition_judgement_setting;
    use_inside_calculate_reserving = 0;
    calculate_new_check_time(); //保证设定写入速度
}
//设置内部换向时间
void change_to_use_inside_calculate_reserving(void)
{
    use_inside_calculate_reserving = 1;
}
//电机方向设定
void set_motor_derection(char motor_derection_in)
{
    if (motor_derection_in)
    {
        motor_status_set(0, 4, 2);
        visual_pole_status_constant[0] = 0;
        visual_pole_status_constant[1] = 4;
        visual_pole_status_constant[2] = 2;
    }
    else
    {
        motor_status_set(0, 2, 4);
        visual_pole_status_constant[0] = 0;
        visual_pole_status_constant[1] = 2;
        visual_pole_status_constant[2] = 4;
    }
    direction_of_motor = motor_derection_in;
}
// pid以及限幅设定
void set_limit_reserving_time(pid_virables *pid_position_controller, double kp_in, double ki_in, double kd_in, u16 limit_reserving_time_in)
{
    limit_reserving_time = limit_reserving_time_in;
    half_limit_reserving_time = limit_reserving_time / 2;
    half_limit_reserving_time_after_subb = half_limit_reserving_time - 25;
    kp_setting = kp_in;
    ki_setting = ki_in;
    kd_setting = kd_in;
    set_pid_params(pid_position_controller, kp_in, ki_in, kd_in, -20, half_limit_reserving_time_after_subb);
}

//状态检测的监视程序
void BLDC_condition_judgement_check_watching()
{
    // count_for_condition_judgement = condition_judgement_calculated;
    //自动换向设定
    count_for_condition_judgement++;
    if (count_for_condition_judgement == (condition_judgement_calculated >> 1))
    {
        BLDC_mid_change_mode_compare();
    }
    else if (count_for_condition_judgement > condition_judgement_calculated)
    {
        count_for_condition_judgement = 0;
        motor_status_change_with_out_check();
        if (calculated_speed_control)
        {
            change_over_zero_param();
            calculated_speed_control--;
        }
        else
        {
            calculate_PID_now = 1;
        }
    }
}
void set_virtual_pole_status(char pole_number, char status)
{
    int16_t pole_error;
    pole_error = visual_pole_status_constant[pole_number] - status;
    visual_pole_status_for_calculation[0] = visual_pole_status_constant[0] - pole_error;
    visual_pole_status_for_calculation[1] = visual_pole_status_constant[1] - pole_error;
    visual_pole_status_for_calculation[2] = visual_pole_status_constant[2] - pole_error;
    if (visual_pole_status_for_calculation[0] < 0)
    {
        visual_pole_status_for_calculation[0] += 6;
    }
    if (visual_pole_status_for_calculation[1] < 0)
    {
        visual_pole_status_for_calculation[1] += 6;
    }
    else if (visual_pole_status_for_calculation[1] >= 6)
    {
        visual_pole_status_for_calculation[1] -= 6;
    }
    if (visual_pole_status_for_calculation[2] < 0)
    {
        visual_pole_status_for_calculation[2] += 6;
    }
    else if (visual_pole_status_for_calculation[2] >= 6)
    {
        visual_pole_status_for_calculation[2] -= 6;
    }
}
int32_t get_time_error_by_voltage_in_calculate(char pole_number, u8 check_flag)
{

    voltage_after_crrection[0] = voltage_adc_1in1[0][visual_pole_status_for_calculation[0]][check_flag];
    voltage_after_crrection[1] = voltage_adc_1in1[1][visual_pole_status_for_calculation[1]][check_flag];
    voltage_after_crrection[2] = voltage_adc_1in1[2][visual_pole_status_for_calculation[2]][check_flag];
    voltage_after_crrection[0] = (voltage_after_crrection[0] * 2004) >> 11;
    voltage_after_crrection[2] = (voltage_after_crrection[2] * 2095) >> 11;
    switch (pole_number)
    {
    case 0:
        return voltage_after_crrection[0] - (voltage_after_crrection[1] >> 1) - (voltage_after_crrection[2] >> 1);
    case 1:
        return voltage_after_crrection[1] - (voltage_after_crrection[0] >> 1) - (voltage_after_crrection[2] >> 1);

    case 2:
        return voltage_after_crrection[2] - (voltage_after_crrection[1] >> 1) - (voltage_after_crrection[0] >> 1);
    }
    return -1;
}
char check_crossing_time_positions(char pole_number)
{
    char rising_time, down_time; // sum_of_rising_down_time;
    int32_t temp_left_voltage, temp_right_voltage, temp_mid_voltage;
    u8 j_left, j_right, k_check, out_loop_for_check = 1;

    j_left = 4;

    if (condition_judgement_calculated < 64)
    {
        j_right = condition_judgement_calculated;
    }
    else if (condition_judgement_calculated < 128)
    {
        j_right = condition_judgement_calculated >> 1;
    }
    else if (condition_judgement_calculated < 256)
    {
        j_right = condition_judgement_calculated >> 2;
    }
    else
    {
        j_right = 63;
    }
    k_check = (j_left + j_right) >> 1;
    //计算下降沿
    set_virtual_pole_status(pole_number, 2); //设定比对值
    VFCES_now = -voltage_for_crossing_electron_specification;
    temp_left_voltage = get_time_error_by_voltage_in_calculate(pole_number, j_left);
    temp_right_voltage = get_time_error_by_voltage_in_calculate(pole_number, j_right);

    if (temp_right_voltage > 0)
    {
        down_time = j_right;
    }
    else if (temp_left_voltage < 0)
    {
        down_time = j_left;
    }
    out_loop_for_check = 1;
    while (out_loop_for_check)
    {
        k_check = (j_left + j_right) >> 1;
        temp_mid_voltage = get_time_error_by_voltage_in_calculate(pole_number, k_check);
        if (temp_mid_voltage < 0)
        {
            j_right = k_check;
        }
        else if (temp_mid_voltage > 0)
        {
            j_left = k_check;
        }
        else
        {
            j_left = k_check;
        }
        if (j_left + 1 >= j_right)
        {
            down_time = j_right;
            out_loop_for_check = 0;
        }
    }
    j_left = 4;
    if (condition_judgement_calculated < 64)
    {
        j_right = condition_judgement_calculated;
    }
    else if (condition_judgement_calculated < 128)
    {
        j_right = condition_judgement_calculated >> 1;
    }
    else if (condition_judgement_calculated < 256)
    {
        j_right = condition_judgement_calculated >> 2;
    }
    else
    {
        j_right = 63;
    }
    k_check = (j_left + j_right) >> 1;
    //计算上升沿
    set_virtual_pole_status(pole_number, 5); //设定比对值
    VFCES_now = voltage_for_crossing_electron_specification;
    temp_left_voltage = get_time_error_by_voltage_in_calculate(pole_number, j_left);
    temp_right_voltage = get_time_error_by_voltage_in_calculate(pole_number, j_right);

    if (temp_right_voltage < 0)
    {
        rising_time = j_right;
    }
    else if (temp_left_voltage > 0)
    {
        rising_time = j_left;
    }
    out_loop_for_check = 1;
    while (out_loop_for_check)
    {
        k_check = (j_left + j_right) >> 1;
        temp_mid_voltage = get_time_error_by_voltage_in_calculate(pole_number, k_check);
        if (temp_mid_voltage > 0)
        {
            j_right = k_check;
        }
        else if (temp_mid_voltage < 0)
        {
            j_left = k_check;
        }
        else
        {
            j_left = k_check;
        }
        if (j_left + 1 >= j_right)
        {
            rising_time = j_right;
            out_loop_for_check = 0;
        }
    }
    return rising_time + down_time;
}
//计算总误差
void all_times_error_sum(void)
{
    frequency_specification = hardware_specification * 60000 * 0.5 / condition_judgement_calculated;
    phase_angle_delay_specification = atan(frequency_specification);
    voltage_for_crossing_electron_specification = 1 * (phase_angle_delay_specification * 6 / 3.1415926 - 1) * 60000 * 0.5 / condition_judgement_calculated;
    time_error_for_crossing_specification = phase_angle_delay_specification / 3.1415926 * 3.00 * (double)condition_judgement_calculated;
    crossing_time_error[0] = check_crossing_time_positions(0);
    crossing_time_error[1] = check_crossing_time_positions(1);
    crossing_time_error[2] = check_crossing_time_positions(2);
    crossing_time_sum = crossing_time_error[0] + crossing_time_error[1] + crossing_time_error[2];
    if (crossing_time_sum > 60)
    {
        if (1)
        {
            crossing_time_sum = crossing_time_sum;
        }
    }
    if (condition_judgement_calculated < 128)
    {
        crossing_time_sum = crossing_time_sum << 1;
    }
    else if (condition_judgement_calculated < 256)
    {
        crossing_time_sum = crossing_time_sum << 2;
    }
    else if (condition_judgement_calculated < 512)
    {
        crossing_time_sum = crossing_time_sum << 3;
    }
    crossing_time_error_sum = (((nagetive_feedback_by_time_error << 1) - crossing_time_sum + (time_error_for_crossing_specification << 1) + time_error_for_crossing_specification) * nagetive_feedback_by_time_error) >> 8;
    if (crossing_time_error_sum < 0 && nagetive_feedback_by_time_error < 100)
    {
        crossing_time_error_sum = crossing_time_error_sum * 1.5;
    }
}
//计算电压误差
void check_rise_down_error_voltage()
{
    // check_voltage_switch_shoked();
    error_rise_down[0] = voltage_adc_1in1[0][5][condition_judgement_calculated - 5] - voltage_adc_1in1[0][2][5];
    error_rise_down[1] = voltage_adc_1in1[1][5][condition_judgement_calculated - 5] - voltage_adc_1in1[1][2][5];
    error_rise_down[2] = voltage_adc_1in1[2][5][condition_judgement_calculated - 5] - voltage_adc_1in1[2][2][5];
    error_out_loop_correction = voltage_adc_1in1[0][5][4] - voltage_adc_1in1[0][2][4] - per_frequency_for_calculation;
    if (error_out_loop_correction < 0)
    {
        error_out_loop_correction *= 2;
    }
    else
    {
        error_out_loop_correction = 0;
    }

    error_sum = ((error_rise_down[0] + error_rise_down[1] + error_rise_down[2]) * (condition_judgement_calculated - 2) * (condition_judgement_calculated - 2));
    error_sum = (error_sum >> 10) + error_out_loop_correction;
    // if (nagetive_feedback_by_time_error > 100 && error_sum > 0)
    // {
    //     error_sum = -1000;
    // }
    if (error_sum < 0)
    {
        error_sum = error_sum << 6;
    }
}
//设定电流目标
void set_current_target_for_torque(int current_target_min_in, int current_target_max_in)
{
    current_target_min = current_target_max_in;
    current_target_max = current_target_max_in;
}
//计算电流误差
void check_certain_stage_error_current(void)
{
    int current_sum[3], current_average[3];
    char loop_times, i_sum_loop, i_pole_loop;
    if (condition_judgement_calculated < 64)
    {
        loop_times = condition_judgement_calculated;
    }
    else if (condition_judgement_calculated < 128)
    {
        loop_times = condition_judgement_calculated >> 1;
    }
    else if (condition_judgement_calculated < 256)
    {
        loop_times = condition_judgement_calculated >> 2;
    }
    else
    {
        loop_times = 63;
    }
    for (i_pole_loop = 0; i_pole_loop < 3; i_pole_loop += 2)
    {
        current_sum[i_pole_loop] = 0;
        for (i_sum_loop = 0; i_sum_loop < loop_times; i_sum_loop++)
        {
            current_sum[i_pole_loop] += current_adc_1in1[i_pole_loop][3][i_sum_loop] + current_adc_1in1[i_pole_loop][4][i_sum_loop];
        }
        current_average[i_pole_loop] = current_sum[i_pole_loop] / loop_times - (current_adc_average_1in1_average[i_pole_loop] << 2);
    }
    sum_of_phase_current = current_average[0] + current_average[2];

    error_of_down_end_sub_middle_current = current_adc_1in1[0][4][loop_times - 1] + current_adc_1in1[2][4][loop_times - 1] - current_adc_1in1[0][4][loop_times >> 2] - current_adc_1in1[2][4][loop_times >> 2];
    if (error_of_down_end_sub_middle_current < 0)
    {
        error_sum_of_current = -((-error_of_down_end_sub_middle_current) << 7) - 20000;
        calculate_pid_output(&pid_voltage_error_controller, -5000);
        calculate_pid_output(&pid_time_set_controller, -500);
    }
    else
    {
        if (sum_of_phase_current < current_target_min)
        {
            error_sum_of_current = current_target_min - sum_of_phase_current;
        }
        else if (error_sum_of_current > current_target_max)
        {
            error_sum_of_current = current_target_max - sum_of_phase_current;
        }
        else
        {
            error_sum_of_current = 0;
        }
    }
}

//以下内容都已经被优化
//误差百分比提升设定
void set_auto_rise_duty_virable(double error_check_double_input, double error_check_number_input, u32 duty_add_per_cycle_input, u32 duty_limit_setting_input)
{
    error_check_double = error_check_double_input;
    error_check_number = error_check_number_input;
    duty_add_per_cycle = duty_add_per_cycle_input;
    duty_limit_setting = duty_limit_setting_input;
}
//误差百分比提升控制

//获取开始阶段电压
void pole_status_begin_get_voltage(void)
{
    pole_begin_voltage[0][pole_status_set.u_pole_status] = voltage_adc_2in1[0]; // pole_status_set.u_pole_status
    pole_begin_voltage[1][pole_status_set.v_pole_status] = voltage_adc_2in1[1];
    pole_begin_voltage[2][pole_status_set.w_pole_status] = voltage_adc_2in1[2];
}
//获取结束阶段电压
void pole_status_end_get_voltage(void)
{
    pole_end_voltage[0][pole_status_set.u_pole_status] = voltage_adc_2in1[0];
    pole_end_voltage[1][pole_status_set.v_pole_status] = voltage_adc_2in1[1];
    pole_end_voltage[2][pole_status_set.w_pole_status] = voltage_adc_2in1[2];
}
void pole_status_mid_get_voltage(void)
{
    pole_mid_voltage[0][pole_status_set.u_pole_status] = voltage_adc_2in1[0];
    pole_mid_voltage[1][pole_status_set.v_pole_status] = voltage_adc_2in1[1];
    pole_mid_voltage[2][pole_status_set.w_pole_status] = voltage_adc_2in1[2];
}
//获取结束阶段电流
void pole_end_get_current(void)
{
    pole_end_current[0][pole_status_set.u_pole_status] = current_adc_2in1[0] - current_adc_average_2in1_average[0];
    pole_end_current[1][pole_status_set.v_pole_status] = current_adc_2in1[1] - current_adc_average_2in1_average[1];
    pole_end_current[2][pole_status_set.w_pole_status] = current_adc_2in1[2] - current_adc_average_2in1_average[2];
}
//获取穿越时间误差
void checked_crossing_error(void)
{
    crossing_time_error[0] = rising_crossing_time[0] + falling_crossing_time[0];
    crossing_time_error[1] = rising_crossing_time[1] + falling_crossing_time[1];
    crossing_time_error[2] = rising_crossing_time[2] + falling_crossing_time[2];
    crossing_time_error_sum = 12 + nagetive_feedback_by_time_error - crossing_time_error[0] - crossing_time_error[1] - crossing_time_error[2];
    if (nagetive_feedback_by_time_error <= 50)
    {
        //   crossing_time_error_sum += calculate_PID_output_by_time_error;
    }
    if (crossing_time_error_sum > 0)
    {
        // crossing_time_error_sum = crossing_time_error_sum >> 2;
    }
}
//检测穿越时间(用新方式检测时间)
void get_and_check_crossing_time(void)
{
    voltage_after_crrection[0] = voltage_adc_2in1[0];
    voltage_after_crrection[1] = voltage_adc_2in1[1];
    voltage_after_crrection[2] = voltage_adc_2in1[2];
    voltage_after_crrection[0] = (voltage_after_crrection[0] * 2004) >> 11;
    voltage_after_crrection[2] = (voltage_after_crrection[2] * 2095) >> 11;
    voltage_for_crossing_flag[0] = voltage_after_crrection[0] - (voltage_after_crrection[1] >> 1) - (voltage_after_crrection[2] >> 1);
    voltage_for_crossing_flag[1] = voltage_after_crrection[1] - (voltage_after_crrection[0] >> 1) - (voltage_after_crrection[2] >> 1);
    voltage_for_crossing_flag[2] = voltage_after_crrection[2] - (voltage_after_crrection[0] >> 1) - (voltage_after_crrection[1] >> 1);
    switch (pole_status_set.u_pole_status)
    {
    case 0: //
        if (checked_crossing_flag[0])
        {
            rising_crossing_time[0] = condition_judgement_calculated;
            checked_crossing_flag[0] = 0;
        }
        break;
    case 1: //
        checked_crossing_flag[0] = 1;
        break;
    case 3:
        if (checked_crossing_flag[0])
        {
            rising_crossing_time[0] = condition_judgement_calculated;
            checked_crossing_flag[0] = 0;
        }
        break;
    case 4: //
        checked_crossing_flag[0] = 1;
        break;
    case 2:
        if (0 < last_voltage_for_crossing_flag[0] && voltage_for_crossing_flag[0] < 0)
        {
            falling_crossing_time[0] = count_for_condition_judgement;
            checked_crossing_flag[0] = 0;
        }
        last_voltage_for_crossing_flag[0] = voltage_for_crossing_flag[0];
        break;
    case 5: //
        if (0 > last_voltage_for_crossing_flag[0] && voltage_for_crossing_flag[0] > 0)
        {
            rising_crossing_time[0] = count_for_condition_judgement;
            checked_crossing_flag[0] = 0;
        }
        last_voltage_for_crossing_flag[0] = voltage_for_crossing_flag[0];
    default:
        break;
    }

    switch (pole_status_set.v_pole_status)
    {
    case 0: //
        if (checked_crossing_flag[1])
        {
            rising_crossing_time[1] = condition_judgement_calculated;
            checked_crossing_flag[1] = 0;
        }
        break;
    case 1: //
        checked_crossing_flag[1] = 1;
        break;
    case 3:
        if (checked_crossing_flag[1])
        {
            rising_crossing_time[1] = condition_judgement_calculated;
            checked_crossing_flag[1] = 0;
        }
        break;
    case 4: //
        checked_crossing_flag[1] = 1;
        break;
    case 2:
        if (0 < last_voltage_for_crossing_flag[1] && voltage_for_crossing_flag[1] < 0)
        {
            falling_crossing_time[1] = count_for_condition_judgement;
            checked_crossing_flag[1] = 0;
        }
        last_voltage_for_crossing_flag[1] = voltage_for_crossing_flag[1];
        break;
    case 5: //
        if (0 > last_voltage_for_crossing_flag[1] && voltage_for_crossing_flag[1] > 0)
        {
            rising_crossing_time[1] = count_for_condition_judgement;
            checked_crossing_flag[1] = 0;
        }
        last_voltage_for_crossing_flag[1] = voltage_for_crossing_flag[1];
    }

    switch (pole_status_set.w_pole_status)
    {
    case 0: //
        if (checked_crossing_flag[2])
        {
            rising_crossing_time[2] = condition_judgement_calculated;
            checked_crossing_flag[2] = 0;
        }
        break;
    case 1: //
        checked_crossing_flag[2] = 1;
        break;
    case 3:
        if (checked_crossing_flag[2])
        {
            rising_crossing_time[2] = condition_judgement_calculated;
            checked_crossing_flag[2] = 0;
        }
        break;
    case 4: //
        checked_crossing_flag[2] = 1;
        break;
    case 2:
        if (0 < last_voltage_for_crossing_flag[2] && voltage_for_crossing_flag[2] < 0)
        {
            falling_crossing_time[2] = count_for_condition_judgement;
            checked_crossing_flag[2] = 0;
        }
        last_voltage_for_crossing_flag[2] = voltage_for_crossing_flag[2];
        break;
    case 5: //
        if (0 > last_voltage_for_crossing_flag[2] && voltage_for_crossing_flag[2] > 0)
        {
            rising_crossing_time[2] = count_for_condition_judgement;
            checked_crossing_flag[2] = 0;
        }
        last_voltage_for_crossing_flag[2] = voltage_for_crossing_flag[2];
    default:
        break;
    }
}
void update_calculate_and_update(void)
{
    if (write_in_calculate_result)
    {
        if (count_for_condition_judgement == 1)
        {
            if (use_inside_calculate_reserving && check_stages_for_calculation == 1) //保证在0时写入并保证计算之前已经写入过一遍
            {                                                                        //注意，分开后出现严重的停车事件
                condition_judgement_calculated = reserving_time_after_correction;
                calculate_new_check_time(); //保证设定写入速度
                write_in_calculate_result = 0;
                check_per_circle_flag = 0; //屏蔽此次计算
                check_stages_for_calculation = 3;
            }
            if (check_stages_for_calculation == 5)
            {
                BLDC_motor_modulation_value_set(duty_calculated_from_current);
                write_in_calculate_result = 0;
                check_per_circle_flag = 0; //屏蔽此次计算
                check_stages_for_calculation++;
                voiding_current_duty_calculated = 1; //屏蔽多次计算
            }
        }
    }

    if (pole_status_set.u_pole_status == 0)
    {
        if (check_per_circle_flag)
        {

            switch (check_stages_for_calculation)
            {
            case 0:
                if (count_for_condition_judgement > half_condition_judgement_calculated)
                {
                    checked_crossing_error();
                    //  check_rise_down_error_voltage();
                    //外部启动恒定时间PID
                    calculate_PID_now = 1;

                    check_per_circle_flag = 0;
                    check_stages_for_calculation++;
                }
                break;

            case 1:
                //为了保证写入速度，此次移到外部
                break;
            case 2:
                check_stages_for_calculation++;
                break;
            case 3:
                if (voiding_current_duty_calculated)
                {
                    check_stages_for_calculation = 0; //屏蔽多次计算以稳定
                    check_per_circle_flag = 0;        //屏蔽此次计算
                    voiding_current_duty_calculated--;
                    break;
                }

                check_rise_down_error_voltage();
                check_stages_for_calculation = 4;

                check_per_circle_flag = 0; //屏蔽此次计算
                break;
            case 4:
                check_certain_stage_error_current();
                calculate_current_PID_now = 1; //开启电流PID计算

                check_per_circle_flag = 0; //屏蔽此次计算
                check_stages_for_calculation++;
                break;
            case 5: //
                // if (count_for_condition_judgement == 1 && write_in_calculate_result) //保证在0时写入并保证计算之前已经写入过一遍
                //注意，分开后出现严重的停车事件
                //为了保证写入速度，此次移到外部

                break;
            case 6: //
            case 7: //
                    // check_stages_for_calculation = 8;
                    // break;
            case 8: //
                voiding_current_duty_calculated = 3;
                check_per_circle_flag = 0; //屏蔽此次计算
                check_stages_for_calculation = 0;
                break;
            default:
                break;
            }
        }
    }
    else
    {
        check_per_circle_flag = 0;
    }
}
void params_set_init(void)
{
}
//每个阶段的首末电压检查
// if (count_for_condition_judgement == check_voltage_diff_flag)
// {
//     pole_status_begin_get_voltage(); //首端时电压检测
// }
// else if (condition_judgement_calculated - count_for_condition_judgement == check_voltage_diff_flag)
// {

//     pole_status_end_get_voltage(); //上升时电压检测
// }
// else if (count_for_condition_judgement == half_condition_judgement_calculated)
// {
//     pole_status_mid_get_voltage();
//     pole_end_get_current();
// }

// if (count_for_condition_judgement >= 4)
// {
//     // get_and_check_crossing_time();
// }
//获取最大值（已被波形上升沿检测优化）
// void get_max_current(void)
// {
//     switch (pole_status_set.u_pole_status)
//     {
//     case 3:
//     case 4:
//     case 5:
//         if (max_current_temp[0] < get_adc_pole_current_2in1_value(0))
//         {
//             max_current_temp[0] = get_adc_pole_current_2in1_value(0);
//         }

//         break;
//     default:
//         if (max_current_temp[0])
//         {
//             max_current_temp_read[0] = max_current_temp[0];
//             max_current_temp[0] = 0;
//         }

//         break;
//     }
//     switch (pole_status_set.v_pole_status)
//     {
//     case 3:
//     case 4:
//     case 5:
//         if (max_current_temp[1] < get_adc_pole_current_2in1_value(1))
//         {
//             max_current_temp[1] = get_adc_pole_current_2in1_value(1);
//         }

//         break;
//     default:
//         if (max_current_temp[1])
//         {
//             max_current_temp_read[1] = max_current_temp[1];
//             max_current_temp[1] = 0;
//         }

//         break;
//     }
//     switch (pole_status_set.w_pole_status)
//     {
//     case 3:
//     case 4:
//     case 5:
//         if (max_current_temp[2] < get_adc_pole_current_2in1_value(2))
//         {
//             max_current_temp[2] = get_adc_pole_current_2in1_value(2);
//         }

//         break;
//     default:
//         if (max_current_temp[2])
//         {
//             max_current_temp_read[2] = max_current_temp[2];
//             max_current_temp[2] = 0;
//         }

//         break;
//     }
// }
//设置检查电压是否是错误升降（为了提高效率，不用循环，直接判断，判断顺序u\v\w,判断方式先上升沿后下降沿）
// void check_voltage_switch_shoked(void)
// {
//     //上升沿判断
//     if (pole_end_voltage[0][5] < pole_begin_voltage[0][5])
//     {
//         pole_begin_voltage[0][5] = pole_end_voltage[0][5];
//     }
//     if (pole_end_voltage[1][5] < pole_begin_voltage[1][5])
//     {
//         pole_begin_voltage[1][5] = pole_end_voltage[1][5];
//     }
//     if (pole_end_voltage[2][5] < pole_begin_voltage[2][5])
//     {
//         pole_begin_voltage[2][5] = pole_end_voltage[2][5];
//     }
//     //下降沿判断
//     if (pole_end_voltage[0][2] > pole_begin_voltage[0][2])
//     {
//         pole_begin_voltage[0][2] = pole_end_voltage[0][2];
//     }
//     if (pole_end_voltage[1][2] > pole_begin_voltage[1][2])
//     {
//         pole_begin_voltage[1][2] = pole_end_voltage[1][2];
//     }
//     if (pole_end_voltage[2][2] > pole_begin_voltage[2][2])
//     {
//         pole_begin_voltage[2][2] = pole_end_voltage[2][2];
//     }
// }
// //获取上升下降状态
// void pole_status_set_check_before_update()
// {

//     rise_down_flat_flag[0][pole_status_set.u_pole_status] = get_rise_down_status(0);
//     rise_down_flat_flag[1][pole_status_set.v_pole_status] = get_rise_down_status(1);
//     rise_down_flat_flag[2][pole_status_set.w_pole_status] = get_rise_down_status(2);
// }
